package com.duyutian.controller.manageController;

import com.duyutian.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by dyt on 2018/10/10.
 */
@Controller
@RequestMapping("/cms")
public class UserCmsController {
    @RequestMapping("/login")
    public String userLogin(HttpServletRequest request,User user,Model model){
        if(user.getUsername()!=null&&user.getPassword()!=null){
            List<User> users = User.dao.find("select * from user where username = ? and password = ? ", user.getUsername(), user.getPassword());
            if(users!=null&&users.size()>0){
                User loginer = users.get(0);
                request.getSession().setAttribute("user",loginer);
                loginer.setLogged(new Date());
                model.addAttribute("user",loginer);
                return "cms/index";
            }else{
                return "cms/login";
            }
        }
        return "cms/login";
    }
}
